package cn.cyh.queue_service.controller;

import cn.cyh.queue_service.common.Result;
import cn.cyh.queue_service.exception.BusinessException;
import cn.cyh.queue_service.model.dto.StatisticsResponse;
import cn.cyh.queue_service.model.entity.Queue;
import cn.cyh.queue_service.model.entity.Statistics;
import cn.cyh.queue_service.mq.event.TakeNumberEvent;
import cn.cyh.queue_service.mq.producer.RabbitEventProducer;
import cn.cyh.queue_service.service.StatisticsService;
import cn.cyh.queue_service.websocket.CurrentQueues;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 取票接口
 *
 * @since 2025-04-10 10:45
 **/
@RestController
@RequestMapping("/queue")
public class FaceQueueController {

    @Resource
    private RabbitEventProducer rabbitEventProducer;

    @Resource
    private StatisticsService statisticsService;

    @RequestMapping("/getTicket")
    @Tag(name = "取号端", description = "开始抢票")
    public Result<Boolean> getTicket(@RequestBody TakeNumberEvent queue) {
        if (CurrentQueues.sessionMap.get(queue.getSessionId()) == null) {
            throw new BusinessException("socket连接失效，请刷新页面！");
        }
        TakeNumberEvent event = new TakeNumberEvent();
        event.setServiceTypeId(queue.getServiceTypeId());
        event.setSessionId(queue.getSessionId());
        rabbitEventProducer.sendTakeNumberEvent(event);
        return Result.success();
    }

    @RequestMapping("reFreshData")
    @Tag(name = "取号端", description = "更新数据")
    public Result<List<StatisticsResponse>> reFreshData(@RequestBody Statistics statistics) {
        return Result.success(statisticsService.getDailyStatistics(statistics.getStatDate(),
                statistics.getServiceTypeId()));
    }

}
